package cn.hznc.controller.service.screen;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.screen.*;
import cn.hznc.domain.request.screen.*;
import cn.hznc.mapper.*;
import cn.hznc.vo.screen.AnnualDataVo;
import cn.hznc.vo.screen.TimeValueVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author：Administrator
 * @Date：2024/3/15 11:56
 */
@Service
public class ScreenService{

    @Resource
    private CncToolOutputMapper cncToolOutputMapper;

    @Resource
    private CostDataMapper costDataMapper;


    @Resource
    private ToolDeliverRateMapper toolDeliverRateMapper;

    @Resource
    private ToolQualifiedRateMapper toolQualifiedRateMapper;

    @Resource
    private QualityQualifiedRateMapper qualityQualifiedRateMapper;

    @Resource
    private ProductionDataMapper productionDataMapper;

    @Resource
    private AnnualDataMapper annualDataMapper;

    private static final ConcurrentHashMap<Integer , String> costDataGroupMap = new ConcurrentHashMap<>();

    static {
        costDataGroupMap.put(0 , "刀具单位成本");
        costDataGroupMap.put(1 , "万元产值耗材");
        costDataGroupMap.put(2 , "万元产值废品损耗");
    }

    public Object editCncToolOutput(EditCncToolOutputReq editCncToolOutputReq) {
        //清除在新增
        cncToolOutputMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editCncToolOutputReq.getEditCncToolOutputReq())){
            cncToolOutputMapper.insertBatchSomeColumn(editCncToolOutputReq.getEditCncToolOutputReq());
        }
        return Boolean.TRUE;
    }

    public Object qryCncToolOutput() {
//        QueryWrapper<CncToolOutputEntity> wrapper = new QueryWrapper<>();
//        wrapper.select("time");
//        wrapper.select("value");
//        return cncToolOutputMapper.selectList(new QueryWrapper<>());
        Map<String , Object> retMap = new HashMap<>();
        retMap.put("name" , "数控刀具产量");
        retMap.put("data" , cncToolOutputMapper.qryCncToolOutput());
        return retMap;
    }

    public Object editCostData(EditDataReq editDataReq) {
        costDataMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editDataReq.getEditCostDataReq())){
            costDataMapper.insertBatchSomeColumn(editDataReq.getEditCostDataReq());
        }
        return Boolean.TRUE;
    }

    public Object qryCostData() {
        List<CostDataEntity> list1 = new ArrayList<>();
        List<CostDataEntity> list2 = new ArrayList<>();
        List<CostDataEntity> list3 = new ArrayList<>();
        List<CostDataEntity> costDataEntities = costDataMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(costDataEntities)){
            costDataEntities.forEach(costDataEntity -> {
                if(ObjectUtil.isNotNull(costDataEntity.getTypeFlag())){
                    if(costDataEntity.getTypeFlag() == 0){
                        list1.add(costDataEntity);
                    }else if(costDataEntity.getTypeFlag() == 1){
                        list2.add(costDataEntity);
                    }else if(costDataEntity.getTypeFlag() == 2){
                        list3.add(costDataEntity);
                    }
                }
            });
        }
        List<HashMap<String, Object>> resultList = new ArrayList<>();
        HashMap<String, Object> retMap = new HashMap<>();
        HashMap<String, Object> retMap1 = new HashMap<>();
        HashMap<String, Object> retMap2 = new HashMap<>();
        retMap.put("name" , costDataGroupMap.get(0));
        retMap.put("data" , list1);
        resultList.add(retMap);
        retMap1.put("name" , costDataGroupMap.get(1));
        retMap1.put("data" , list2);
        resultList.add(retMap1);
        retMap2.put("name" , costDataGroupMap.get(2));
        retMap2.put("data" , list3);
        resultList.add(retMap2);
        return resultList;
    }

    public Object editToolDeliverRate(EditToolDeliverRateReq editToolDeliverRateReq) {
        //清除在新增
        toolDeliverRateMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editToolDeliverRateReq.getEditToolDeliverRateReq())){
            toolDeliverRateMapper.insertBatchSomeColumn(editToolDeliverRateReq.getEditToolDeliverRateReq());
        }
        return Boolean.TRUE;
    }

    public Object qryToolDeliverRate() {
        List<ToolDeliverRateEntity> list1 = new ArrayList<>();
        List<ToolDeliverRateEntity> list2 = new ArrayList<>();
        List<ToolDeliverRateEntity> list3 = new ArrayList<>();
        List<ToolDeliverRateEntity> toolDeliverRateEntities = toolDeliverRateMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(toolDeliverRateEntities)){
            toolDeliverRateEntities.forEach(toolDeliverRateEntity -> {
                if(ObjectUtil.isNotNull(toolDeliverRateEntity.getTypeFlag())){
                    if(toolDeliverRateEntity.getTypeFlag() == 0){
                        list1.add(toolDeliverRateEntity);
                    }else if(toolDeliverRateEntity.getTypeFlag() == 1){
                        list2.add(toolDeliverRateEntity);
                    }else if(toolDeliverRateEntity.getTypeFlag() == 2){
                        list3.add(toolDeliverRateEntity);
                    }
                }
            });
        }
        List<HashMap<String, Object>> resultList = new ArrayList<>();
        HashMap<String, Object> retMap = new HashMap<>();
        HashMap<String, Object> retMap1 = new HashMap<>();
        HashMap<String, Object> retMap2 = new HashMap<>();
        retMap.put("name" , "车刀");
        retMap.put("data" , list1);
        resultList.add(retMap);
        retMap1.put("name" , "铣刀");
        retMap1.put("data" , list2);
        resultList.add(retMap1);
        retMap2.put("name" , "钻削");
        retMap2.put("data" , list3);
        resultList.add(retMap2);
        return resultList;
    }

    public Object editToolQualifiedRate(EditToolQualifiedRateReq editToolQualifiedRateReq) {
        //清除在新增
        toolQualifiedRateMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editToolQualifiedRateReq.getEditToolQualifiedRateReq())){
            toolQualifiedRateMapper.insertBatchSomeColumn(editToolQualifiedRateReq.getEditToolQualifiedRateReq());
        }
        return Boolean.TRUE;
    }

    public Object qryToolQualifiedRate() {
        List<ToolQualifiedRateEntity> list1 = new ArrayList<>();
        List<ToolQualifiedRateEntity> list2 = new ArrayList<>();
        List<ToolQualifiedRateEntity> list3 = new ArrayList<>();
        List<ToolQualifiedRateEntity> list4 = new ArrayList<>();
        List<ToolQualifiedRateEntity> list5 = new ArrayList<>();
        List<ToolQualifiedRateEntity> list6 = new ArrayList<>();
        List<ToolQualifiedRateEntity> toolQualifiedRateEntities = toolQualifiedRateMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(toolQualifiedRateEntities)){
            toolQualifiedRateEntities.forEach(toolQualifiedRateEntity -> {
                if(ObjectUtil.isNotNull(toolQualifiedRateEntity.getTypeFlag())){
                    if(toolQualifiedRateEntity.getTypeFlag() == 0){
                        list1.add(toolQualifiedRateEntity);
                    }else if(toolQualifiedRateEntity.getTypeFlag() == 1){
                        list2.add(toolQualifiedRateEntity);
                    }else if(toolQualifiedRateEntity.getTypeFlag() == 2){
                        list3.add(toolQualifiedRateEntity);
                    }else if(toolQualifiedRateEntity.getTypeFlag() == 3){
                        list4.add(toolQualifiedRateEntity);
                    }else if(toolQualifiedRateEntity.getTypeFlag() == 4){
                        list5.add(toolQualifiedRateEntity);
                    }else if(toolQualifiedRateEntity.getTypeFlag() == 5){
                        list6.add(toolQualifiedRateEntity);
                    }
                }
            });
        }
        List<HashMap<String, Object>> resultList = new ArrayList<>();
        HashMap<String, Object> retMap = new HashMap<>();
        HashMap<String, Object> retMap1 = new HashMap<>();
        HashMap<String, Object> retMap2 = new HashMap<>();
        HashMap<String, Object> retMap3 = new HashMap<>();
        HashMap<String, Object> retMap4 = new HashMap<>();
        HashMap<String, Object> retMap5 = new HashMap<>();

        retMap.put("name" , "车刀");
        retMap.put("data" , list1);
        resultList.add(retMap);

        retMap1.put("name" , "切断切槽工具");
        retMap1.put("data" , list2);
        resultList.add(retMap1);

        retMap2.put("name" , "套式铣刀");
        retMap2.put("data" , list3);
        resultList.add(retMap2);

        retMap3.put("name" , "螺纹车刀");
        retMap3.put("data" , list4);
        resultList.add(retMap3);

        retMap4.put("name" , "钻削刀具");
        retMap4.put("data" , list5);
        resultList.add(retMap4);

        retMap5.put("name" , "杆式铣刀");
        retMap5.put("data" , list6);
        resultList.add(retMap5);
        return resultList;
    }

    public Object editQualityQualifiedRate(EditQualityQualifiedRateReq editQualityQualifiedRateReq) {
        //清除在新增
        qualityQualifiedRateMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editQualityQualifiedRateReq.getEditQualityQualifiedRateReq())){
            qualityQualifiedRateMapper.insertBatchSomeColumn(editQualityQualifiedRateReq.getEditQualityQualifiedRateReq());
        }
        return Boolean.TRUE;
    }

    public Object qryQualityQualifiedRate() {
        Map<String , Object> retMap = new HashMap<>();
        retMap.put("name" , "质量合格率");
        retMap.put("data" , qualityQualifiedRateMapper.selectList(new QueryWrapper<>()));
        return retMap;
    }

    public Object editProductionData(EditProductionDataReq editProductionDataReq) {
        //清除在新增
        productionDataMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editProductionDataReq.getEditProductionDataReq())){
            productionDataMapper.insertBatchSomeColumn(editProductionDataReq.getEditProductionDataReq());
        }
        return Boolean.TRUE;
    }

    public Object qryProductionData() {
        return productionDataMapper.selectList(new QueryWrapper<>());
    }

    public Object editAnnualData(EditAnnualDataReq editAnnualDataReq) {
        //清除在新增
        annualDataMapper.delete(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(editAnnualDataReq.getEditAnnualDataReq())){
            annualDataMapper.insertBatchSomeColumn(editAnnualDataReq.getEditAnnualDataReq());
        }
        return Boolean.TRUE;
    }

    public Object qryAnnualData() {
        List<AnnualDataVo> resultList = new ArrayList<>();
        List<AnnualDataEntity> annualDataEntities = annualDataMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(annualDataEntities)){
            annualDataEntities.forEach(annualDataEntity -> {
                AnnualDataVo annualDataVo = new AnnualDataVo();
                annualDataVo.setName(annualDataEntity.getName());
                annualDataVo.setUnit(annualDataEntity.getUnit());
                annualDataVo.setValue(annualDataEntity.getValue());
                Map<String , Object> params = new HashMap<>();
                params.put("typeFlag" , annualDataEntity.getTypeFlag());
                params.put("dataValue" , annualDataEntity.getDataValue());
                annualDataVo.setData(params);
                resultList.add(annualDataVo);
            });
        }
        return resultList;
    }
}
